package com.sk.leetcode.arithmetic.test1_10;

/**
 * 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数：
 * <p>
 * 输入: s = "PAYPALISHIRING", numRows = 4
 * 输出: "PINALSIGYAHRPI"
 * 解释:
 * <p>
 * P     I    N
 * A   L S  I G
 * Y A   H R
 * P     I
 */
public class Test6 {
    public static void main(String[] args) {
        String str = convert("ab", 2);
        System.out.println(str);
    }

    public static String convert(String s, int numRows) {
        if(numRows == 1){
            return s;
        }
        String[] strs = s.split("");
        int len = strs.length;
        StringBuilder builder = new StringBuilder();
        boolean dir = true;
        int n = 0, r = 0;
        for (int i = 0; i < len; i++) {
            builder.append(strs[n]);
            if(r == 0 || r == (numRows-1)) {
                n = n + (numRows - 1) * 2;
            } else {
                if(dir) {
                    dir = false;
                    n = n + (numRows - r - 1) * 2;
                } else {
                    dir = true;
                    n = n + r * 2;
                }
            }
            if(n >= len) {
                r++;
                n = r;
                dir = true;
            }
        }

        return builder.toString();
    }
}
